<template>
  <div class="search">
    <h2>探索这个世界...</h2>

    <div class="search-input">
      <input type="text" placeholder="你在寻找什么..." v-model="str" />
    </div>

    <div class="search-result">
      <ul>
        <router-link
          tag="li"
          :to="'/articles/' + item.objectId"
          v-for="item in searchArr"
          :key="item.objectId"
        >
          <div class="agopages-img">
            <img :src="item.topImage.url" />
          </div>

          <div class="agopages-text">
            <p>{{ item.title }}</p>
            <span>{{ item.body }}</span>
          </div>
        </router-link>
      </ul>
    </div>

    <div class="search-text">
      <p>大家都在搜</p>
      <ul class="hotWord">
        <li v-for="item in textArr" :key="item.objectId" @click="getWord">
          {{ item.word }}
        </li>
      </ul>
    </div>

    <div class="dianzi"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      textArr: "",
      str: "",
      searchArr: "",
    };
  },
  methods: {
    getData() {
      this.$axios
        .get(`http://cloud.agopage.com/6b83fd3bc06260ec/mostSearch`)
        .then(({ data }) => {
          this.textArr = data.mostSearchs;
        });
    },
    getRecommend(word) {
      this.$axios
        .get(`http://cloud.agopage.com/6b83fd3bc06260ec/search?keyword=${word}`)
        .then(({ data }) => {

          this.searchArr = data.agopages;
        });
    },

    getWord(e) {
      this.str = e.target.textContent.trim();
    },
  },
  watch: {
    str(curVal) {
      if (curVal == "") {
        this.searchArr = "";
      } else {
        // 实现input连续输入，只发一次请求
        clearTimeout(this.timeout);

        this.timeout = setTimeout(() => {
          this.getRecommend(curVal);
        }, 30);
      }
    },
  },

  created() {
    this.getData();
  },
};
</script>

<style lang="scss" scoped>
.search {
  height: 100vh;
  background-color: rgba(242, 238, 238, 0.742);
}

.search h2 {
  font-size: 18px;
  padding: 30px;
}

.search-input {
  width: 85%;
  height: 200px;
  border-radius: 5px;
  margin: 10px auto;
  background-image: url(./3.webp.jpg);
  background-size: 100% 100%;
  position: relative;

  input {
    position: absolute;
    width: 97%;
    height: 30px;
    border-radius: 10px;
    border-color: pink;
    bottom: 10px;
  }
}

.search-text {
  margin-top: 35px;
  p {
    margin-left: 25px;
  }
}

.hotWord {
  display: flex;
  color: rgba(62, 173, 62, 0.731);
  font-size: 17px;
  flex-wrap: wrap;
  margin-top: 15px;
  margin-left: 15px;

  li {
    width: 15%;
    margin: 5px;
  }
}

.search-result {
  position: absolute;
  width: 99%;

  top: 285px;
  background-color: #fff;
  li {
    display: flex;
    justify-content: space-between;
    width: 88%;
    height: 100px;
    margin: 10px auto;
    border-bottom: 1px solid #ccc;
  }

  .agopages-img {
    width: 36%;
    img {
      width: 100%;
      height: 80px;
      border-radius: 15px;
    }
  }

  .agopages-text {
    width: 54%;

    p {
      color: #ccc;
      font-weight: 600;
      overflow: hidden; /*内容超出后隐藏*/
      text-overflow: ellipsis; /* 超出内容显示为省略号 */
      white-space: nowrap; /* 文本不进行换行 */
      padding-top: 5px;
    }

    span {
      font-size: 13px;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
      overflow: hidden;
      padding-top: 8px;
    }
  }
}

.dianzi {
  height: 50px;
}
</style>>
